//
// Created by andrew on 2022/5/17.
//

#include <vector>
#include <algorithm>
#include <iostream>
#include <gtest/gtest.h>
using namespace std;

class Solution {
public:
    static vector<vector<int>> threeSum(vector<int>& nums) {
        int n = nums.size();
        // 先将所有数据进行排序
        sort(nums.begin(), nums.end());
        vector<vector<int>> ans;
        for (int first = 0; first < n; ++first) {
            // 保证和第一次枚举的数据不相同
            if (first > 0 && nums[first] == nums[first - 1]) {
                continue;
            }
            // 最后一个指向数字的最右端
            int third = n - 1;
            int target = -nums[first];
            for (int second = first + 1; second < third;) {
                // 第三个数也要保证和三次枚举的不相同, 只要保证后一个和前一个不相同
                if (second > first + 1 && nums[second] == nums[second - 1]) {
                    second ++;
                    continue;
                }
                // 需要保证b指针在C指针的左侧
                if (nums[second] + nums[third] > target) {
                    --third;
                }
                if (nums[second] + nums[third] < target) {
                    second ++;
                }

                if (second == third) {
                    break;
                }
//                nums[second] != nums[third]
                if (  nums[second] + nums[third] == target) {
                    ans.push_back({nums[first], nums[second], nums[third]});
                    second ++;
                }
            }
        }

        return ans;
    }
};

class Solution1 {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        vector<vector<int>> ans;
        // 枚举 a
        for (int first = 0; first < n; ++first) {
            // 需要和上一次枚举的数不相同
            if (first > 0 && nums[first] == nums[first - 1]) {
                continue;
            }
            // c 对应的指针初始指向数组的最右端
            int third = n - 1;
            int target = -nums[first];
            // 枚举 b
            for (int second = first + 1; second < n; ++second) {
                // 需要和上一次枚举的数不相同
                if (second > first + 1 && nums[second] == nums[second - 1]) {
                    continue;
                }
                // 需要保证 b 的指针在 c 的指针的左侧
                while (second < third && nums[second] + nums[third] > target) {
                    --third;
                }
                // 如果指针重合，随着 b 后续的增加
                // 就不会有满足 a+b+c=0 并且 b<c 的 c 了，可以退出循环
                if (second == third) {
                    break;
                }
                if (nums[second] + nums[third] == target) {
                    ans.push_back({nums[first], nums[second], nums[third]});
                }
            }
        }
        return ans;
    }
};

class Solution2 {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        vector<vector<int>> results;
        for (int i = 0; i < nums.size(); ++i) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            int target = -nums[i], left = i + 1, right = nums.size() - 1;
            while (left < right) {
                if (nums[left] + nums[right] == target) {
                    results.push_back({nums[i], nums[left], nums[right]});
                    // 防止多个重复的数字出现
                    while (left < right && nums[left] == nums[left + 1]) ++left;
                    while (left < right && nums[right] == nums[right - 1]) --right;

                    ++left; --right;
                } else if (nums[left] + nums[right] < target) {
                    ++left;
                } else {
                    --right;
                }
            }
        }
        return results;
    }
};







TEST(threeSum15, threeSum) {

    std::vector<int> v0 = {-1,0,1,2,-1,-4};

    auto vec = Solution::threeSum(v0);
    for (const auto& v : vec) {
        for (auto d : v) {
            std::cout << d << std::endl;
        }
    }


    //    EXPECT_STREQ("da" , Solution::threeSum(strs));

}


